package com.ssd.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ssd.api.product.ProductSkuApi;
import com.ssd.api.product.dto.SkuEntity;
import com.ssd.mapper.OrderDetailMapper;
import com.ssd.entity.OrderDetailEntity;
import com.ssd.service.OrderDetailService;
import com.ssd.view.BaseResponse;
import com.ssd.viewmodel.OrderDetailViewModel;
import com.ssd.viewmodel.OrderDetailViewVOModel;
import com.ssd.vo.SkuInfoVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * OrderDetailServiceImpl 类
 *
 * @title:
 * @requirement:
 * @ClassName:
 * @author:首雯静
 * @creationTime:2024-11-22/15:03
 * @version: 1.0
 */
@Service
public class OrderDetailServiceImpl extends ServiceImpl<OrderDetailMapper,OrderDetailEntity> implements OrderDetailService {

    @Resource
    private ProductSkuApi productSkuApi;//远程调用对象

    /**
     * 查找订单的所有商品
     * @param orderId
     * @return
     */
    @Override
    public List<OrderDetailViewModel> getListByOrderId(Long orderId) {
        List<OrderDetailEntity> orderDetailList = baseMapper.selectByOrderId(orderId);//该订单包含的商品id
        //转换为viewModel对象输出
        List<OrderDetailViewModel> orderDetailViewModels = new ArrayList<>();
        for (OrderDetailEntity orderDetail : orderDetailList) {
            OrderDetailViewModel orderDetailViewModel = new OrderDetailViewModel();
            //查询商品详细信息
            BaseResponse<SkuEntity> skuById1 = productSkuApi.getSkuById1(orderDetail.getSkuId());
            SkuEntity skuEntity = skuById1.getData();
            orderDetailViewModel.setClassId(skuEntity.getClassId());
            orderDetailViewModel.setOrderId(orderId);
            orderDetailViewModel.setPrice(skuEntity.getPrice());
            orderDetailViewModel.setNumber(orderDetail.getNumber());
            orderDetailViewModel.setSkuId(skuEntity.getSkuId());
            orderDetailViewModel.setProductName(skuEntity.getSkuName());
            orderDetailViewModel.setProductSize(skuEntity.getUnit());
            orderDetailViewModels.add(orderDetailViewModel);
        }
        return orderDetailViewModels;
    }

    @Override
    public List<OrderDetailViewVOModel> getListByOrderIdA(Long orderId) {
        List<OrderDetailEntity> orderDetailList = baseMapper.selectByOrderId(orderId);//该订单包含的商品id
        //转换为viewModel对象输出
        List<OrderDetailViewVOModel> orderDetailViewModels = new ArrayList<>();
        for (OrderDetailEntity orderDetail : orderDetailList) {
            OrderDetailViewVOModel orderDetailViewModel = new OrderDetailViewVOModel();
            //查询商品详细信息
            BaseResponse<SkuEntity> skuById1 = productSkuApi.getSkuById1(orderDetail.getSkuId());
            SkuEntity skuEntity = skuById1.getData();
            orderDetailViewModel.setClassId(skuEntity.getClassId());
            orderDetailViewModel.setOrderId(orderId);
            orderDetailViewModel.setPrice(skuEntity.getPrice());
            orderDetailViewModel.setQuantity(orderDetail.getNumber());
            orderDetailViewModel.setSkuId(skuEntity.getSkuId());
            orderDetailViewModel.setSkuName(skuEntity.getSkuName());
            orderDetailViewModel.setUnit(skuEntity.getUnit());
            orderDetailViewModel.setSkuImage(skuEntity.getSkuImage());
            orderDetailViewModels.add(orderDetailViewModel);
        }
        return orderDetailViewModels;
    }

    @Override
    public List<com.ssd.api.order.dto.OrderDetailViewModel> getListByOrderId1(Long orderId) {
        List<OrderDetailEntity> orderDetailList = baseMapper.selectByOrderId(orderId);//该订单包含的商品id
        //转换为viewModel对象输出
        List<com.ssd.api.order.dto.OrderDetailViewModel> orderDetailViewModels = new ArrayList<>();
        for (OrderDetailEntity orderDetail : orderDetailList) {
            com.ssd.api.order.dto.OrderDetailViewModel orderDetailViewModel = new com.ssd.api.order.dto.OrderDetailViewModel();
            //查询商品详细信息
            BaseResponse<SkuEntity> skuById1 = productSkuApi.getSkuById1(orderDetail.getSkuId());
            SkuEntity skuEntity = skuById1.getData();
            orderDetailViewModel.setClassId(skuEntity.getClassId());
            orderDetailViewModel.setOrderId(orderId);
            orderDetailViewModel.setPrice(skuEntity.getPrice());
            orderDetailViewModel.setNumber(orderDetail.getNumber());
            orderDetailViewModel.setSkuId(skuEntity.getSkuId());
            orderDetailViewModel.setProductName(skuEntity.getSkuName());
            orderDetailViewModel.setProductSize(skuEntity.getUnit());
            orderDetailViewModels.add(orderDetailViewModel);
        }
        return orderDetailViewModels;
    }

    /**
     * 批量插入订单商品
     * @param skuList
     * @param orderId
     * @return
     */
    @Override
    public Boolean insertList(List<SkuInfoVO> skuList, Long orderId) {
        List<OrderDetailEntity> orderDetailEntityList = new ArrayList<>();
        // 调用商品服务查询商品信息
        for (SkuInfoVO skuInfoVO : skuList) {
            BaseResponse<SkuEntity> skuById1 = productSkuApi.getSkuById1(skuInfoVO.getSkuId());
            SkuEntity data = skuById1.getData();
            //转换为实体类
            OrderDetailEntity orderDetailEntity = new OrderDetailEntity();
            orderDetailEntity.setOrderId(orderId);
            orderDetailEntity.setPrice(data.getPrice());
            orderDetailEntity.setClassId(data.getClassId());
            orderDetailEntity.setSkuName(data.getSkuName());
            orderDetailEntity.setNumber(skuInfoVO.getQuantity());
            orderDetailEntity.setSkuId(skuInfoVO.getSkuId());
            orderDetailEntityList.add(orderDetailEntity);
        }
        //批量插入数据库
        boolean saved = this.saveBatch(orderDetailEntityList);
        return saved;
    }
}
